﻿using KeZhou.Models.Instances;
using KeZhou.ViewModels;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace KeZhou.Views.Components
{
    /// <summary>
    /// LoginControl.xaml 的交互逻辑
    /// </summary>
    public partial class LoginControl : UserControl
    {
        public LoginControl()
        {
            InitializeComponent();
            this.DataContext = LoginViewModel.Instance;
        }

        /*
         * 切换控件
         */
        public delegate void SwitchControlEventHandler(object sender, ControlType controlType);
        public event SwitchControlEventHandler SwitchControl;

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SwitchControl?.Invoke(this, ControlType.SignUp);
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SwitchControl?.Invoke(this, ControlType.ResetPassword);
        }

        /*
         * Password不允许绑定，使用传值
         */
        private void PasswordBox_PasswordChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            PasswordBox passwordBox = (PasswordBox)sender;
            LoginViewModel.Instance.Password = passwordBox.Password;
        }

        //登录按钮
        private void Border_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            if (Application.Current.MainWindow is MainWindow mainWindow)
            {
                if (string.IsNullOrEmpty(account.Text) || string.IsNullOrEmpty(password.Password))
                {
                    mainWindow.showWrongMsg(2);
                }
                else
                {
                    mainWindow.showWrongMsg(LoginViewModel.Instance.loginSure());
                }
            }
        }
    }
}
